import {ComponentOptions, PropType} from "vue/types/options";
import typeOfUtils from "../../../utils/type-of-utils";
import {TableItemConfig} from "../TableType";
import {DynamicTable} from "../index.vue.d";

export const TableItemOtherCommonMixin: ComponentOptions<any> = {
  components: {
    RenderVNode: () => import('../../../components/RenderVnodes/index.vue'),
  },
  props: {
    tableItem: {
      type: Object as PropType<TableItemConfig>,
      default: () => {
      }
    },
    vm: {
      type: Object as PropType<DynamicTable>,
      default: () => {
      }
    }
  },
  computed: {
    vxeTable() {
      return this.vm.$refs.tableRef
    },
    size(){
      return this.vxeTable.size
    }
  },
  methods: {
    itemFormatter(tableItem) {
      if (tableItem.formatter) {
        if (typeOfUtils.isFunction(tableItem.formatter)) {
          const vm = this.vm
          return ({cellValue, column, row}) => {
            if (tableItem.formatter) {
              return tableItem.formatter({
                cellValue, column, row,
                rowIndex: vm.getRowIndex(row),
                columnIndex: vm.getColumnIndex(column),
              })
            }
            return cellValue
          }
        } else {
          return tableItem.formatter
        }
      } else {
        return null
      }
    },
    __hasSlot(name) { // 判断是否有插槽
      const vm = this.vm
      const i = this.tableItem
      if (i.typeSlots) { // 支持函数
        if (i.typeSlots(vm.$createElement, i)[name]) {
          return true
        }
      }
      let slotName = i.slotName || i.prop
      if (vm.$scopedSlots[slotName + '_' + name]) {
        return true
      }
      return false
    },
    __getSlot(name, props) { // 渲染插槽
      const vm = this.vm
      const i = this.tableItem
      if (i.typeSlots) { // 支持函数
        return i.typeSlots(vm.$createElement, i)[name]?.(props)
      }
      let slotName = i.slotName || i.prop
      return vm.$scopedSlots[slotName + '_' + name]?.({
          props: {
            ...props,
            $index: props.$rowIndex,
          },
          item: i,
        }) // 支持模板语法
        || []
    },
  }
}
